﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AfterCaveStory
{
    class World
    {
        public float gravidade;
        private List<List<int>> map;
        public Camera2d camera;
        public List<PhysicsObject> objectList = new List<PhysicsObject>();



        public World(float gravidade)
        {
            camera = new Camera2d();
            this.gravidade = gravidade;
        }

        public void createObject(PhysicsObject newObject)
        {
            objectList.Add(newObject);
        }

        public void removeObject(PhysicsObject physicsObject)
        {
            for (int i = 0; i < objectList.Count; i++)
            {
                if (objectList[i] == physicsObject)
                {
                    objectList.Remove(physicsObject);
                }
            }
        }

        public void update()
        {
            for (int i = 0; i < objectList.Count; i++)
            {
                objectList[i].update(this);
            }
        }

        public void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.End();

            spriteBatch.Begin(SpriteSortMode.FrontToBack,
                        BlendState.AlphaBlend,
                        null,
                        null,
                        null,
                        null,
                        camera.get_transformation(Game1.Self.graphics /*Send the variable that has your graphic device here*/));

            for (int i = 0; i < objectList.Count; i++)
            {
                objectList[i].draw(gameTime, spriteBatch);
            }
            spriteBatch.End();
            
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
        }

        public List<List<int>> Map
        {
            get
            {
                return map;
            }
            set
            {
                this.map = value;

                createMap();
            }
        }

        public void createMap()
        {
            for (int v = 0; v < map.Count; v++)
            {
                for (int h = 0; h < map[v].Count; h++)
                {
                    switch (map[v][h])
                    {
                        case 1: 
                            Platform plataforma = new Platform(Game1.Self.Content.Load<Texture2D>("platform"), new Rectangle(0, 0, 32, 32), 0.2f);
                            plataforma.y = 16 + (v * 32);
                            plataforma.x = 16 + (h * 32);
                            //plataforma.drawCollisionBounds = true;
                            plataforma.setarPontoDeReferencia(Objeto2D.PontoDeReferencia.CENTRO);
                            createObject(plataforma);
                            break;

                    }
                }
            }
        }

    }
}
